package com.ys.service.finance.impl;

import com.alibaba.fastjson.JSON;
import com.ys.dto.AddDto;
import com.ys.entity.finance.TcwDepositPlatform;
import com.ys.entity.sys.TsUserChange;
import com.ys.mapper.finance.TcwDepositPlatformMapper;
import com.ys.service.finance.TcwDepositPlatformService;
import com.ys.util.AESUtil;
import com.ys.util.shiro.ShiroUtils;
import com.ys.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service("tcwDepositPlatformService")
public class TcwDepositPlatformServiceImpl implements TcwDepositPlatformService {

    @Autowired
    private TcwDepositPlatformMapper tcwDepositPlatformMapper;

    /**
     * 新增
     * @param params
     * @return
     * @throws Exception
     */
    @Override
    public R add(Map<String, Object> params) throws Exception {
        // 加密保存
        TcwDepositPlatform tcwDepositPlatform = JSON.parseObject(JSON.toJSONString(params), TcwDepositPlatform.class);
        tcwDepositPlatform.setApiCode(AESUtil.aesEncrypt(tcwDepositPlatform.getApiCode()))
                          .setAuthorizationTonken(AESUtil.aesEncrypt(tcwDepositPlatform.getAuthorizationTonken()));
        String loginUserId = ShiroUtils.getUserId();
        TsUserChange tsUserChange = new TsUserChange().setUserId(loginUserId).setStatus(1).queryFirst();
        tcwDepositPlatform.setStatus(1);
        tcwDepositPlatformMapper.insert(tcwDepositPlatform);
        return R.ok(AddDto.closeAndReload());
    }

    /**
     * 修改
     * @param params
     * @return
     * @throws Exception
     */
    @Override
    public R edit(Map<String, Object> params) throws Exception {


        // 加密保存
        TcwDepositPlatform tcwDepositPlatform = JSON.parseObject(JSON.toJSONString(params), TcwDepositPlatform.class);
        tcwDepositPlatform.setApiCode(AESUtil.aesEncrypt(tcwDepositPlatform.getApiCode()))
                .setAuthorizationTonken(AESUtil.aesEncrypt(tcwDepositPlatform.getAuthorizationTonken()));

        tcwDepositPlatformMapper.update(tcwDepositPlatform);
        return R.ok(AddDto.closeAndReload());
    }



}
